#include <QApplication>

#include <QIcon>
#include <QMessageBox>
#include <QSettings>
#include "DataModel.h"
#include "WinMain.h"
#include "scImpl.h"

#define FCT "main"
int main(int argc, char *argv[])
{
    dwkUtil::Trc::inst()->setLevel(dwkUtil::Trc::LOOP); // default is none - and make sure that the singleton is created
	Q_INIT_RESOURCE(sycli);
	QApplication app(argc, argv);
	ctrc(Trc::SETUP,APP,FCT)<<"style "<<app.styleSheet().toAscii().data()<<endl;
	DwkUtil::inst()->registerExceptionCause(APP, EXC_UNKNOWNTRANSACTION, "unknown transaction ID", false);
	DwkUtil::inst()->registerExceptionCause(APP, EXC_LOCALCONFAILED, "local database connection failed", false);

	QCoreApplication::setOrganizationName("Sybil-PM");
	QCoreApplication::setOrganizationDomain("sybil-pm.org");
	QCoreApplication::setApplicationName("Sybil Client");
	QSettings settings;
	if(!settings.contains("identity"))
	{
		settings.setValue("identity", QVariant("Sybil Dedicated Client"));
		ostringstream os;
		os <<"V"<<VERSION_MAJOR<<"."<<VERSION_MINOR<<"."<<VERSION_BUILD;
		settings.setValue("version", QVariant(os.str().c_str()));
		settings.setValue("server/isLocal", QVariant(true));
		settings.setValue("server/localDb", QVariant("sybilLocal.db"));
		settings.setValue("server/host", QVariant("127.0.0.1"));
		settings.setValue("server/port", QVariant(8080));
		settings.setValue("server/useHttps", QVariant(false));
		QList<QVariant> vws;
		vws << QVariant(1) << QVariant(0);
		settings.setValue("views/show", QVariant(vws));
		settings.sync();
	}
	app.setWindowIcon(QIcon(":/res/logo.png"));
	WinMain window;
	//app.connect(&app, SIGNAL(aboutToQuit ()), &window, SLOT(terminateThreads()));
	window.show();
	int res(0);
	try	{res=app.exec();}
	catch(AppException * ex)
	{
		res=ex->getCauseCode();
		QMessageBox::critical(0, QString("Unrecoverable Error"), QString().fromAscii(ex->getMessage()->c_str()));
	}
	return res;
}
